package com.idp.web.shop.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.model.Response;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.shop.entity.Site;
import com.idp.web.shop.service.SiteService;

/**
 * 站点信息controller
 * @author WuJiangWu
 * @date 2018年4月12日上午12:59:19
 */
@Controller
@RequestMapping("/site")
public class SiteController extends BaseController {

	private Logger logger = Logger.getLogger(SiteController.class);

	@Resource
	private SiteService siteService;
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	初始化查询页面
	 * @return
	 */
	@RequestMapping("/init")
	public String init(){
		
		return "shop/site/siteSearch";
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	分页查询
	 * @param site
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Site site,Page<Site> page,HttpServletRequest request){
		try {
			request.setAttribute("page", siteService.findByPage(site, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shop/site/siteList";
	}

	/**
	 * 	2018-04-12 WuJiangWu
	 * 	新增修改页面初始化
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/site")
	public String site(String id,HttpServletRequest request){
		try {
			if(ValidateUtils.isNotEmpty(id)){
				Site site = siteService.getById(id);
				request.setAttribute("site", site);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shop/site/site";
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	保存
	 * @param site
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Response save(Site site){
		Response json = new Response();
		try {
			// 修改
			if(ValidateUtils.isNotEmpty(site.getId())){
				siteService.update(site);
			}
			// 新增
			else{
				siteService.add(site);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("保存失败");
		}
		return json;
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Response delete(String id){
		Response json = new Response();
		try {
			siteService.delete(id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("刪除失败");
		}
		return json;
	}
}
